package cn.yr.blog.controller.management;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.blog.service.ArticleService;
import cn.yr.common.core.domain.dto.YrArticleTagDto;
import cn.yr.common.core.domain.entity.YrArticle;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import cn.yr.common.security.annotation.PreAuthorize;
import cn.yr.search.api.ArticleSearchApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 文章
 * @author Lang 1102076808@qq.com
 * @date 2020-07-15 22:52:20
 */
@Api(tags = "management文章")
@RestController
@RequestMapping("/management/article")
public class ManagerArticleController {

	@Autowired
    private ArticleService articleService;
    @Resource
    private ArticleSearchApi articleSearchApi;
    @Value("article-search")
    private Boolean articleSearch;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize(hasPermi = "manager:article:list")
    public Ret list(@RequestBody PageVo<YrArticleTagDto> pageVo){
	    Map<String, Object> params = pageVo.getParams();
	    QueryWrapper<YrArticleTagDto> wrapper = new QueryWrapper<>();
	    wrapper.orderByDesc("art.recommend","create_time");
	    wrapper.like(ObjectUtil.isNotEmpty(params.get("title")),"art.title",params.get("title"));
	    wrapper.like(ObjectUtil.isNotEmpty(params.get("author")),"art.author",params.get("author"));
	    wrapper.eq(ObjectUtil.isNotEmpty(params.get("type")),"art.type",params.get("type"));
	    wrapper.eq(ObjectUtil.isNotEmpty(params.get("recommend")),"art.recommend",params.get("recommend"));
	    wrapper.eq(ObjectUtil.isNotEmpty(params.get("publish")),"art.publish",params.get("publish"));
	    PageVo<YrArticleTagDto> pageArticle = articleService.pageArticle(pageVo,wrapper);
        return Ret.success(pageArticle);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @PreAuthorize(hasPermi = "manager:article:info")
    public Ret info(@PathVariable("id") Integer id){
		YrArticle yrArticle = articleService.getById(id);
	    return Ret.success(yrArticle);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    @PreAuthorize(hasPermi = "manager:article:add")
    public Ret save(@Validated @RequestBody YrArticleTagDto yrArticleTagDto){
		articleService.addArticle(yrArticleTagDto);
        if(articleSearch){
            articleSearchApi.addArticle(yrArticleTagDto);
        }
	    return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @PreAuthorize(hasPermi = "manager:article:edit")
    public Ret update(@Validated @RequestBody YrArticle yrArticle){
		articleService.updateById(yrArticle);
        if(articleSearch){
            articleSearchApi.updateArticle(yrArticle);
        }
	    return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove/{ids}")
    @PreAuthorize(hasPermi = "manager:article:remove")
    public Ret delete(@PathVariable Integer[] ids){
		articleService.removeByIds(Arrays.asList(ids));
        if(articleSearch){
            for (int i = 0; i < ids.length; i++) {
                articleSearchApi.deleteArticle(ids[i].longValue());
            }
        }
        return Ret.success();
    }

}
